using AntDesign;
using AntDesign.TableModels;
using Microsoft.AspNetCore.Components.Web;
using SE.PO.CInterface.DBProtocols.Devices;
using SE.PO.CInterface.Enums;
using SE.PO.CInterface.Web.Pages.DB.Components;

namespace SE.PO.CInterface.Web.Pages.DB.Device
{
    public partial class DeviceTable
    {
        private AddDeviceFrom _addFrom = default!;
        private SyncDeviceFrom _syncFrom = default!;
        private EditDeviceFrom _editFrom = default!;
        private DelDeviceFrom _delFrom = default!;
        private SelectDeviceType _selectDeviceTypeComp = null!;
        #region query

        private DeviceTypeEnum _selectDeviceType;

        private string _searchString;

        List<DeviceDto> _source = new();

        IEnumerable<DeviceDto>? _selectedRows;

        ITable? _table;

        int _pageIndex = 1;
        int _pageSize = 10;
        int _total;

        protected override async Task OnInitializedAsync()
        {
            await GetSourceAsync(_pageIndex, _pageSize);
        }

        public async Task OnChange(QueryModel<DeviceDto> queryModel)
        {
            await GetSourceAsync(_pageIndex, _pageSize);
        }

        public async Task OnSelectedItemChanged()
        {
            _source = (await Client.GetDevicesByDeviceType(_selectDeviceType) ?? Array.Empty<DeviceDto>()).ToList();
        }

        private async Task GetDeviceSourceByTypeAsync(string s)
        {
            try
            {
                _loadingData = true;
                _source = (await Client.GetDevicesByDeviceType(s) ?? Array.Empty<DeviceDto>()).ToList();
                _total = _source.Count;
                _pageSize= _source.Count;
                StateHasChanged();
                _loadingData = false;
            }
            catch (System.Exception e)
            {
                _loadingData = false;
                throw;
            }
        }
        
        public async Task GetSourceAsync(int pageIndex, int pageSize)
        {
            try
            {
                _loadingData = true;
                _source = (await Client.GetDevicesByDeviceType(_selectDeviceTypeComp?.DeviceType??"") ?? Array.Empty<DeviceDto>()).ToList();
                _total = _source.Count;
                StateHasChanged();
                _loadingData = false;
            }
            catch (System.Exception ex)
            {
                _ = Notice.Error(new()
                {
                    Duration = 100,
                    Message = "异常",
                    Description = ex.Message
                });
            }
            finally
            {
                _loadingData = false;
            }
        }

        #endregion

        #region sync

        bool _syncVisible;

        private void SyncDevicesAssertId()
        {
            _syncVisible = true;
        }

        private void SyncDevices()
        {
            _syncVisible = true;
        }

        private void SyncHandleOk(MouseEventArgs e)
        {
            _syncFrom.Form.Submit();
        }

        private void SyncHandleCancel(MouseEventArgs e)
        {
            Console.WriteLine(e);
            _syncVisible = false;
        }

        #endregion

        #region add

        bool _addVisible;

        private void AddHandleOk(MouseEventArgs e)
        {
            _addFrom.Form.Submit();
        }

        private void AddHandleCancel(MouseEventArgs e)
        {
            Console.WriteLine(e);
            _addVisible = false;
        }

        #endregion

        #region edit

        private CreateUpdateDeviceDto? _editModel;
        bool _editVisible;

        private void EditHandleOk(MouseEventArgs e)
        {
            _editFrom.Form?.Submit();
        }

        private void EditHandleCancel(MouseEventArgs e)
        {
            Console.WriteLine(e);
            _editVisible = false;
        }

        private void Edit(string id)
        {
            var dto = _source.FirstOrDefault(r => r.Id == id);
            if (dto != null)
            {
                _editModel = new CreateUpdateDeviceDto
                {
                    Id = dto.Id,
                    DevAssetId = dto.DevAssetId,
                    RoomId = dto.RoomId,
                    SiteId = dto.SiteId,
                    SuId = dto.SuId,
                    Equipment = dto.Equipment,
                    DeviceDesc = dto.DeviceDesc,
                    DeviceType = dto.DeviceType,
                    DeviceModel = dto.DeviceModel,
                    LocateNeStatus = dto.LocateNeStatus,
                    ScId = dto.ScId
                };
            }
        }

        #endregion

        #region delete

        bool _delVisible;

        private bool _loadingData = true;

        private DeviceDto? DelModel { get; set; } = new();


        private List<DeviceDto>? DelModels { get; set; } = new();

        private void DeleteHandleOk(MouseEventArgs e)
        {
            _delFrom.Form?.Submit();
        }

        private void DeleteHandleCancel(MouseEventArgs e)
        {
            Console.WriteLine(e);
            _delVisible = false;
        }


        private void DeleteSelectedRows()
        {
            DelModels = _selectedRows?.ToList();
            if (DelModels != null && DelModels.Any())
            {
                DelModel = DelModels.FirstOrDefault();
                _delVisible = true;
            }
        }

        private void Delete(string id)
        {
            DelModel = _source.FirstOrDefault(r => r.Id == id);
        }

        #endregion

      
    }
}